package 标准接口;



import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import fc.FcHttpCon;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;



public class DeleteRequestinfo {

//第一步做一次就行了  记录下来
    /**
     * 模拟缓存服务
     */
    private static final Map<String,String> SYSTEM_CACHE = new HashMap <>();

    /**
     * ecology 系统发放的授权许可证(appid)
     */
    private static final String APPID = "EEAA5436-7577-4BE0-8C6C-89E9D44801EA";


    public static void main(String[] args) {

        String requestids="1260058" ;

        String del_reqeustd[]=requestids.split(",");

        for( int i=0;i<del_reqeustd.length;i++){
            HttpRequest.closeCookie();
            String param="requestId="+del_reqeustd[i]+"&otherParams={\"ismonitor\":\"1\"}";
            testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/deleteRequest",param);
        }
        System.out.println(del_reqeustd.length);

    }
    /**
     * 第一步：
     *
     * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
     */

    public static Map<String,Object> testGetoken(String address){
        // 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息




        String secret ="6a393956-42c1-4b75-8b77-91c342ed90f1";
        String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmIqeh3HlAxMNSjepuY1YsRj6pqfX/r88v1xOKI7+vIXDpMLgmaA8ytRGTyHaxscXiXxcCKE6T+z54qrxaxw6b4/WSfcH8QaghHhQRAtvMWxP/SC1Vg5dniQMUVvrsB2wbf0DfyiftGeBf8gJbe1uWsXrMZLZE3olrDMCjylo8SwIDAQAB";

        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
        //ECOLOGY 返回的系统密钥
        SYSTEM_CACHE.put("SERVER_SECRET",secret);


        RSA rsa = new RSA(null,spk);
        //对秘钥进行加密传输，防止篡改数据
        String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口进行注册
        String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
                .header("appid",APPID)
                .header("secret",encryptSecret)
                .header("time","3600")
                .execute().body();
        System.out.println("testGetoken()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY 返回的 token
        // TODO 为 Token 缓存设置过期时间
        SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
        return datas;
    }

    public static String testRestful(String address,String api,String jsonParams){
        //ECOLOGY 返回的 token
        String token = (String) testGetoken(address).get("token");

        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");


        //System.out.println(spk);

        //封装请求头参数
        RSA rsa = new RSA(null,spk);
        //对用户信息进行加密传输,暂仅支持传输 OA 用户 ID
        String encryptUserid = rsa.encryptBase64(FcHttpCon.DEL_USERID,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口
        String data = HttpRequest.post(address + api)
                .header("appid",APPID)
                .header("token",token)
                .header("userid",encryptUserid)
                .body(jsonParams)
                .execute().body();

        // System.out.println("url:"+address + api);
        //System.out.println("appid:"+APPID);
        System.out.println("token:"+token);
        System.out.println("userid:"+encryptUserid);

        System.out.println("testRestful()："+data);

        Object succesResponse = JSON.parse(data);
        Map map = (Map)succesResponse;
        System.out.println(map.get("code"));

        return data;
    }



}
